home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / getgrent.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  220 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)getgrent.c  5.9 (Berkeley) 4/1/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define KERNEL
  39. #include "ixnet.h"
  40. #include <sys/types.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <grp.h>
  44.  
  45. static FILE *_gr_fp;
  46. static struct group _gr_group;
  47. static int _gr_stayopen;
  48. static int grscan(), start_gr();
  49.  
  50. #define MAXGRP        200
  51. static char *members[MAXGRP];
  52. #define MAXLINELENGTH    1024
  53.  
  54. struct group *
  55. getgrent(void) {
  56.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  57.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  58.     return UG_getgrent(p->u_UserGroupBase);
  59.     }
  60.     if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
  61.     return(NULL);
  62.     return(&_gr_group);
  63. }
  64.  
  65. struct group *
  66. getgrnam(const char *name) {
  67.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  68.  
  69.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  70.     struct group *err = UG_getgrnam(p->u_UserGroupBase,name);
  71.  
  72.     if (!err) {
  73.         errno = ug_GetErr(p->u_UserGroupBase);
  74.     }
  75.     return err;
  76.     }
  77.     else {
  78.     int rval;
  79.     if (!start_gr())
  80.         return(NULL);
  81.  
  82.     rval = grscan(1, 0, name);
  83.     if (!_gr_stayopen)
  84.         endgrent();
  85.  
  86.     return(rval ? &_gr_group : NULL);
  87.     }
  88. }
  89.  
  90. struct group *
  91. getgrgid(gid_t gid) {
  92.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  93.  
  94.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  95.     struct group *err;
  96.     err = UG_getgrgid(p->u_UserGroupBase,gid);
  97.  
  98.     if (!err) {
  99.         errno = ug_GetErr(p->u_UserGroupBase);
  100.     }
  101.     return err;
  102.     }
  103.     else {
  104.     int rval;
  105.  
  106.     if (!start_gr())
  107.         return(NULL);
  108.  
  109.     rval = grscan(1, gid, NULL);
  110.     if (!_gr_stayopen)
  111.         endgrent();
  112.  
  113.     return(rval ? &_gr_group : NULL);
  114.     }
  115. }
  116.  
  117. static int
  118. start_gr(void) {
  119.     if (_gr_fp) {
  120.     rewind(_gr_fp);
  121.     return(1);
  122.     }
  123.     return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
  124. }
  125.  
  126. int
  127. setgrent(void) {
  128.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  129.  
  130.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  131.     return UG_setgrent(p->u_UserGroupBase);
  132.     }
  133.     return setgroupent(0);
  134. }
  135.  
  136. int
  137. setgroupent(int stayopen) {
  138.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  139.  
  140.     if (p->u_networkprotocol != IX_NETWORK_AMITCP) {
  141.     if (!start_gr())
  142.         return(0);
  143.     _gr_stayopen = stayopen;
  144.     }
  145.     return 1;
  146. }
  147.  
  148. void
  149. endgrent(void) {
  150.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  151.  
  152.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  153.     UG_endgrent(p->u_UserGroupBase);
  154.     }
  155.     if (_gr_fp) {
  156.     (void)fclose(_gr_fp);
  157.     _gr_fp = NULL;
  158.     }
  159. }
  160.  
  161. static int
  162. grscan(int search, int gid, char *name) {
  163.     register char *cp, **m;
  164.     char *bp;
  165.     char *fgets(), *strsep(), *index();
  166.     static char line[MAXLINELENGTH];
  167.  
  168.     for (;;) {
  169.     if (!fgets(line, sizeof(line), _gr_fp))
  170.         return(0);
  171.     bp = line;
  172.     /* skip lines that are too big */
  173.     if (!index(line, '\n')) {
  174.         int ch;
  175.  
  176.         while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
  177.         ;
  178.  
  179.         continue;
  180.     }
  181.     _gr_group.gr_name = strsep(&bp, ":\n");
  182.     if (search && name && strcmp(_gr_group.gr_name, name))
  183.         continue;
  184.  
  185.     _gr_group.gr_passwd = strsep(&bp, ":\n");
  186.     if (!(cp = strsep(&bp, ":\n")))
  187.         continue;
  188.  
  189.     _gr_group.gr_gid = atoi(cp);
  190.     if (search && name == NULL && _gr_group.gr_gid != gid)
  191.         continue;
  192.  
  193.     for (m = _gr_group.gr_mem = members;; ++m) {
  194.         if (m == &members[MAXGRP - 1]) {
  195.         *m = NULL;
  196.         break;
  197.         }
  198.  
  199.         if ((*m = strsep(&bp, ", \n")) == NULL)
  200.         break;
  201.     }
  202.     return(1);
  203.     }
  204.     /* NOTREACHED */
  205. }
  206.  
  207.  
  208. int
  209. setgroups (int ngroups, const int *gidset)
  210. {
  211.     register struct ixnet *p = u.u_ixnet;
  212.  
  213.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  214.     return UG_setgroups(p->u_UserGroupBase,ngroups,gidset);
  215.     }
  216.     else
  217.   return (ngroups >= 1) ? 0 : -1;
  218. }
  219.  
  220.